package com.k4rthik.clipall.clipb;

import com.k4rthik.clipall.commons.Logger;

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;

import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: k4rthik
 * Date: 3/25/12
 * Time: 2:13 PM
 */

public class ClipOps
{
    public static String getClipboardText(Clipboard clipB)
    {
        Transferable contents = clipB.getContents(null);
        return getClipboardText(contents);
    }

    public static String getClipboardText(Transferable contents)
    {
        String clipboardContents = "";

        boolean hasTransferableText = ((contents != null) &&
                (contents.isDataFlavorSupported(DataFlavor.stringFlavor)));
        if (hasTransferableText) {
            try {
                clipboardContents = (String)contents.getTransferData(DataFlavor.stringFlavor);
            } catch (UnsupportedFlavorException e){
                Logger.log("Bad clipboard contents", e);
            } catch (IOException ex) {
                Logger.log("", ex);
            }
        }
        return clipboardContents;
    }
}
